/**
 * 注册
 */
function onSign() {
	const sign = document.querySelector('.sign_wraper form')
	const userInput = document.querySelector('input[name="username"]')
	const passwordInput = document.querySelector('input[name="password"]')
	// 失去焦点事件 x2
	userInput.addEventListener("blur",function(e){
		e = e || window.event
		let username = userInput.value
		const usernameP = document.querySelector(".usernameP")
		usernameP.innerHTML = ""
		// 非空验证
		if(username == ""){
			usernameP.style.color = "red"
			usernameP.innerHTML = "用户名不能为空"
			return
		}
		// // 正则表达验证
		// let regexp = /^\w{6,12}$/;  //定义正则表达式
		// let flag = regexp.test(username);  //正则表达式校验字符串
		// if(!flag){
		// 	usernameP.style.color = "red"
		// 	usernameP.innerHTML = "您输入的数据格式有误 字符在6-12位之间";
		// 	return
		// }else {
		// 	usernameP.style.color = "green"
		//  	usernameP.innerHTML = "√";
		// 	return
		// }

	})
	passwordInput.addEventListener("blur",function(e){
		e = e || window.event
		let password = passwordInput.value
		const passwordP = document.querySelector(".passwordP")
		passwordP.innerHTML = ""
		// 非空验证
		if(password == ""){
			passwordP.style.color = "red"
			passwordP.innerHTML = "密码不能为空"
			return
		}
		// 正则表达验证
		let regexp = /^\w{6,12}$/;  //定义正则表达式
		let flag = regexp.test(password);  //正则表达式校验字符串
		if(!flag){
			passwordP.style.color = "red"
			passwordP.innerHTML = "您输入的数据格式有误 字符在6-12位之间";
			return
		}else {
			passwordP.style.color = "green"
			passwordP.innerHTML = "√";
			return
		}
	})



	// 表单提交事件
	sign.addEventListener('submit', function (e) {
		e = e || window.event
		e.preventDefault() // 阻止默认行为
		let username = userInput.value
		let password = passwordInput.value
		// 调用注册接口注册用户
		requestRegister(username, password)
			.then(res => {
				alert('注册成功!')
			})
			.catch(err => {
				alert(err)
			})
	})
}

onSign()
